<?php
class DigFile_ResourceController extends Zend_Controller_Action
{    
    public function treeDataAction() {
        $json = null;
        $id = $this->getRequest()->getParam('id', 0);
        if($id != '0') {
            //die('dir');
            $dir = new DigFile_Model_Symlink($this->_getPath($id));
        } else {
            //die('default');
            $dir = DigFile_Model_Symlink::getRootDir();
        }
        $children = $dir->children();
        if(count($children) > 0) {
            foreach ($children as $child) {
                if(! $child->isHidden()) {
                    $childData = array();
                    $childData['data'] = $child->name;
                    if($child->isDir()) {
                        $childData['state'] = 'closed';
                    }
                    $childData['attributes'] = array(
                        'id' => $this->_getId($child->filepath()),
                        'content_type'  => $child->namespace,
                        'class' => $child->namespace,
                        'type'  => ($child->isDir()) ? 'dir' : 'file',
                        'ext'   => $child->getExtension()
                        );
                    $json[] = $childData;
                }
            }
        }
        $this->_helper->json($json);
    }

    public function editAction() {

    }

    public function renameAction() {
        $id = $this->getRequest()->getParam('id', 0);
        $name = $this->getRequest()->getParam('name', null);
        $resource = DigFile_Model_Resource::factory($this->_getPath($id));
        $resource->rename($name);
        $this->_helper->json($resource->toArray());
    }

    public function moveAction() {
        $id = $this->getRequest()->getParam('id', 0);
        $path = $this->getRequest()->getParam('path', null);
        //Zend_Debug::dump($this->_getPath($id));die();
        $resource = DigFile_Model_Resource::factory($this->_getPath($id));
        $parent = DigFile_Model_Resource::factory($this->_getPath($path));
        $resource->move($parent);
        $this->_helper->json($resource->toArray());
    }

    public function discardAction() {
        $id = $this->getRequest()->getParam('id', 0);
        $resource = DigFile_Model_Resource::factory($this->_getPath($id));
        $result = $resource->discard();
        $this->_helper->json($result);
    }

    protected function _getId($path) {
        return $path;
        return str_replace('/', $this->_separator, $path);
    }

    protected function _getPath($id) {
        return $id;
        return str_replace($this->_separator, '/', $id);
    }
}
?>
